package com.bee.bootstrap.system.controller;

import com.bee.bootstrap.system.service.IAttachmentService;
import com.bee.common.core.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/upload")
@Api(value = "/upload", description = "附件上传")
public class UploadController {

    @Autowired
    private IAttachmentService attachmentServiceImpl;

    @PostMapping("/files")
    @ApiOperation(value="多附件上传", notes="多附件上传")
    public Result uploadFiles(HttpServletRequest request, @RequestParam(required = false, defaultValue = "",name = "busType") @NotBlank(message = "业务类型不能为空") String busType) throws IOException {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        return attachmentServiceImpl.uploadFiles(files,busType);
    }

    @PostMapping("/{id}/custom")
    @ApiOperation(value="自定义附件名称", notes="自定义附件名称")
    public Result customFileName(@PathVariable(name = "id") Long id,@RequestParam("customFileName") String customFileName){
        return attachmentServiceImpl.updateCustomFileName(id,customFileName);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value="删除附件记录", notes="删除附件记录")
    public Result customFileName(@PathVariable(name = "id") Long id){
        return attachmentServiceImpl.deleteAttachment(id);
    }



}
